import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';
import $ from 'jquery';
import MFS from '@/common/mfs_oauth.js';



export default {
  name: 'oauth',
  components: {},
  data () {
    return {

    }
  },
  mounted () {
    //this.authorization();
    sessionStorage.setItem("studentId",'-1')
    sessionStorage.setItem("siteId",'-1')
    sessionStorage.setItem("mobile",'-1')
    sessionStorage.setItem("companyId",'-1')
    this.forward('-1')
  },
  methods: {
    forward(siteId) {
      if (siteId == null || siteId == '-1') {
        this.$router.push({path: '/site'})
      } else {
        sessionStorage.setItem('siteId',siteId)
        this.$router.push({path: '/Hello',query:{siteId: siteId}})
      }
    },
    getUserInfo(openId,accessToken,refreshToken) {
      //alert(openId+'**************'+accessToken + '************' + refreshToken)
      sessionStorage.setItem("openId",openId)
      let _this = this
      http.post(CONSTANT.PAYMENT_API + '/app/oauthChannel', {clientId: CONSTANT.clientId,openId:openId,accessToken:accessToken,refreshToken:refreshToken}, (res) => {
          //alert(JSON.stringify(res))
        sessionStorage.setItem("mobile",res.mobileNo)
        sessionStorage.setItem("studentId",'-1')
        sessionStorage.setItem("siteId",'-1')
        sessionStorage.setItem("mobile",'-1')
        sessionStorage.setItem("companyId",'-1')
        _this.forward('-1')
      })
    },
    oauth() {
      //alert('oauth方法')
      var _this = this
      MFS.Oauth.getOauthCode({
        "callback":"MFS.Oauth.getOauthCodeCallback",
        "params":{
          "clientId":CONSTANT.clientId,
          //"redirectUri":"bm.qsng.cn/eduplatform/html/nx/index.html#/Hello",
          "redirectUri":"bm.qsng.cn/eduplatform/html/nx/index.html#/oauth",
          "state":"1",
          "scopeList":[{
            "scopeName":"get_associate_user_info"
          }]
        }
      });
      window.MFS.Oauth.getOauthCodeCallback = function(result) {
        var obj = JSON.parse(result);
        //alert('getOauthCode回调'+JSON.stringify(result))
        if (obj.rstCode == 902) {
          MFS.goBack()
          return
        }
        http.post(CONSTANT.PAYMENT_API + '/app/getClientSign', {dataStr:obj.code}, (res) => {
          MFS.Oauth.getAccessToken({
          "callback":"MFS.Oauth.getAccessTokenCallback",
          "params":{
            "clientSign":res.result,
            "code":obj.code,
            "clientId":CONSTANT.clientId
          }
        });
        window.MFS.Oauth.getAccessTokenCallback = function(result) {
           //alert('&&&&&&&&&&&&&&'+JSON.stringify(result))
          var obj = JSON.parse(result);
          MFS.Oauth.getOpenId({
            "callback":"MFS.Oauth.getOpenIdCallback",
            "params":{
              "clientId":CONSTANT.clientId,
              "accessToken":obj.accessToken,
              "refreshToken":obj.refreshToken,
              "scopeList":[{
                "scopeName":"get_associate_user_info"
              }]
            }
          });
          window.MFS.Oauth.getOpenIdCallback = function(result) {
            //alert(JSON.stringify(result))
            var objResult = JSON.parse(result);
            if (objResult.openId == null || objResult.openId == '') {
              //alert('授权失败：3')
            } else {
              _this.getUserInfo(objResult.openId,obj.accessToken,obj.refreshToken);
            }
          }
        }
      })
      }
    },
    authorization () {
      //alert(0)
      var _this = this
      MFS.Oauth.queryOpenId({
        "callback":"MFS.Oauth.queryOpenIdCallback",
        "params":{
          "clientId":CONSTANT.clientId
        }
      });
      window.MFS.Oauth.queryOpenIdCallback = function(result) {
        var obj = JSON.parse(result);
        //alert(JSON.stringify(result))
        if (obj.openId != null) {
          if (obj.isExpired == 0) {
            sessionStorage.setItem("openId",obj.openId)
            //http.post(CONSTANT.GETTOKEN, {clientId: CONSTANT.clientId,openId:obj.openId}, (res) => {
            //  //alert(JSON.stringify(res))
            //  _this.getUserInfo(obj.openId,res.accessToken)
            // })
            if (obj.openId == null || obj.openId == '') {
              //alert('授权失败：1')
              return;
            }
            sessionStorage.setItem("studentId",'-1')
            sessionStorage.setItem("siteId",'-1')
            sessionStorage.setItem("mobile",'-1')
            sessionStorage.setItem("companyId",'-1')
            _this.forward('-1')
          } else {
            http.post(CONSTANT.PAYMENT_API + '/app/getToken', {clientId: CONSTANT.clientId,openId:obj.openId}, (res) => {
              //alert(JSON.stringify(res))
              var refreshToken = res.refreshToken
              if (refreshToken != undefined && refreshToken != null && refreshToken != '') {
              http.post(CONSTANT.PAYMENT_API + '/app/getClientSign', {dataStr:refreshToken}, (res2) => {
                MFS.Oauth.refreshAccessToken({
                "callback":"MFS.Oauth.refreshAccessTokenCallback",
                "params":{
                  "clientId":CONSTANT.clientId,
                  "clientSign":res2.result,
                  "refreshToken":refreshToken,
                  "openId":obj.openId,
                  "scopeList":[{
                    "scopeName":"get_associate_user_info"
                  }]
                }
              });
              MFS.Oauth.refreshAccessTokenCallback = function(result) {
                var objRefreshAccessToken = JSON.parse(result);
                 //alert(JSON.stringify(result)+'======'+objRefreshAccessToken.accessToken)
                if (objResult.openId == null || objResult.openId == '') {
                  //alert('授权失败：2')
                } else {
                  _this.getUserInfo(obj.openId,objRefreshAccessToken.accessToken,objRefreshAccessToken.refreshToken)
                }
              }
            })
            } else {
              _this.oauth();
            }
          })
          }
        } else {
          // alert('else')
          _this.oauth();
        }
      };
    }
  }
}
